import { Channel, Token, History } from "@/types/data";

const TOKEN_KEY = 'geek-h5-sh98-token'

const CHANNEL_KEY = 'geek-app-channel-98'

// 搜索关键字的本地缓存键名
const SEARCH_HISTORY_KEY = 'geek-search-history'

/**
 * 从缓存获取搜索历史关键字
 */
export const getLocalHistories = (): History => {
  return JSON.parse(localStorage.getItem(SEARCH_HISTORY_KEY) || '[]')
}

/**
 * 将搜索历史关键字存入本地缓存
 * @param {Array} histories
 */
export const setLocalHistories = (histories: History): void => {
  localStorage.setItem(SEARCH_HISTORY_KEY, JSON.stringify(histories))
}

/**
 * 删除本地缓存中的搜索历史关键字
 */
export const removeLocalHistories = () => {
  localStorage.removeItem(SEARCH_HISTORY_KEY)
}

// 保存Token
export function setToken(token: Token): void {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(token));
}

// 获取Token
export function getToken(): Token {
  return JSON.parse(localStorage.getItem(TOKEN_KEY) || '{}');
}

// 删除Token
export function removeToken(): void {
  localStorage.removeItem(TOKEN_KEY);
}

// 判断有无Token
export function hasToken(): boolean {
  return !!getToken().token
}

export function setChannel(channels: Channel[]): void {
  localStorage.setItem(CHANNEL_KEY, JSON.stringify(channels));
}

export function getChannel(): Channel[] {
  return JSON.parse(localStorage.getItem(CHANNEL_KEY) || '[]')
}